package com.study.task;

import com.study.entity.SysJob;
import com.study.service.SysJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author
 */
//持久化
@PersistJobDataAfterExecution
//禁止并发执行(Quartz不要并发地执行同一个job定义（这里指一个job类的多个实例）)
@DisallowConcurrentExecution
@Slf4j
public class TestTask3 extends QuartzJobBean implements Job {


    @Autowired
    private SysJobService sysJobService;

    @Override
    public void executeInternal(JobExecutionContext context) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("开始执行任务3... ..."+sdf.format(new Date()));
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));

        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        if(Objects.nonNull(jobDataMap)){
            jobDataMap.entrySet().forEach(entry->{
                log.info("任务3 jobDataMap:{}",entry.getKey()+","+entry.getValue());
            });
        }

        HashMap<String, String> map = new HashMap();
        map.put("jobGroup", "mail");
        map.put("jobStatus", "1");
        List<SysJob> jobList = sysJobService.querySysJobList(map);
        if (Objects.isNull(null) || jobList.size() == 0) {
            //log.info("没有状态为可用的发送邮件任务... ...");
            return;
        }

        return;
    }
}
